package com.mzk.qBittorrentPush.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mzk.qBittorrentPush.common.Const;
import com.mzk.qBittorrentPush.entity.HttpClientResult;
import com.mzk.qBittorrentPush.entity.QBittorrentEntity;
import com.mzk.qBittorrentPush.service.PushService;
import com.mzk.qBittorrentPush.util.Base64Utils;
import com.mzk.qBittorrentPush.util.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ ProjectName: qBittorrentPush
 * @ Author     ：miaozhenkai
 * @ Date       ：Created in 2020/10/10 18:23
 * @ Description：推送service
 * @ Modified By：
 * @ Version:     1.0
 */
@Slf4j
@Service
public class PushServiceImpl implements PushService {


    @Override
    public String push(QBittorrentEntity qBittorrentEntity) throws Exception {
        Boolean wechatPushFlag = wechatPush(qBittorrentEntity);
        log.info("wechatPushFlag:{}", wechatPushFlag);
        return null;
    }

    @Override
    public Boolean wechatPush(QBittorrentEntity qBittorrentEntity) throws Exception {
        Boolean flag = false;
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("text", "qBittorrent下载完成");
        paramMap.put("desp", "####详细信息：\n" +
                "- qBittorrent 客户端 ip：" + Const.QBITTORRENT_CLIENT_IP + "\n" +
                "- Torrent 名称：" + qBittorrentEntity.getTorrentName() + "\n" +
                "- 分类：" + qBittorrentEntity.getCategory() + "\n" +
                "- 标签：" + qBittorrentEntity.getTags() + "\n" +
                "- 内容路径：" + qBittorrentEntity.getContentPath() + "\n" +
                "- 根目录：" + qBittorrentEntity.getRootPath() + "\n" +
                "- 保存路径：" + qBittorrentEntity.getSavePath() + "\n" +
                "- 文件数：" + qBittorrentEntity.getNumberOfFiles() + "\n" +
                "- Torrent 大小（字节）：" + qBittorrentEntity.getTorrentSizeBytes() + "\n" +
                "- 当前 tracker：" + qBittorrentEntity.getCurrentTracker() + "\n" +
                "- 哈希值：" + qBittorrentEntity.getInfoHash());
        HttpClientResult httpClientResult = HttpClientUtils.doPost(Base64Utils.decoderBase64(Const.WECHAT_PUSH_URL_BASE64), paramMap);
        if (httpClientResult.getCode() == 200) {
            Map<String, Object> bodyMap = JSONObject.parseObject(httpClientResult.getContent(), LinkedHashMap.class);
            log.info("返回body:{}", bodyMap);
            if (0 == Integer.valueOf(bodyMap.get("errno").toString())) {
                flag = true;
            }
        }
        System.out.println(httpClientResult);
        return flag;
    }

    @Override
    public Boolean emailPush(QBittorrentEntity qBittorrentEntity) {
        return null;
    }
}
